package com.dw.widget.slideshow.utils;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;

import com.dw.widget.slideshow.db.helper.ImageDBHelper;
import com.dw.widget.slideshow.db.object.ImageData;
import com.dw.widget.slideshow.objects.Size;

public class AppCommon {

	
	private static ApplicationEx _APP = ApplicationEx.INSTANCE();
	
	/**
	 * 插入单个图片文件到数据库
	 */
	public static void insertImage2Db(ImageDBHelper dbHelper, Context context, File file)
	{
		if(dbHelper == null)
		{
			dbHelper = new ImageDBHelper(context, _APP.DB_NAME, ImageData.class);
		}
		
		DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
		
		String path = file.getAbsolutePath();
		
		ImageData data = new ImageData();
		data.setName(file.getName());
		data.setPicturePath(path);
		
		Date takeTime = new Date(file.lastModified());
		data.setTakeTime(formater.format(takeTime));
		
		data.setEnabled(ApplicationEx.INSTANCE().YES);
		
		Size size = ImageUtils.getResolution(path);
		if(size.getWidth()/size.getHeight() >= _APP.widgetWidthPx/_APP.widgetHeightPx)   // 较宽
		{
			data.setOrientation(_APP.HORIZONTAL);
		}
		else
		{
			data.setOrientation(_APP.VERTICAL);
		}

		dbHelper.insert(data);
	}
	
	/**
	 * 插入多个图片文件到数据库
	 */
	public static void insertImages2Db(Context context, File[] imagePath)
	{
		DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
		ImageDBHelper dbHelper = new ImageDBHelper(context, _APP.DB_NAME, ImageData.class);
		for(File f : imagePath)
		{
			String path = f.getAbsolutePath();
			
			ImageData data = new ImageData();
			data.setName(f.getName());
			data.setPicturePath(path);
			
			Date takeTime = new Date(f.lastModified());
			data.setTakeTime(formater.format(takeTime));
			
			data.setEnabled(ApplicationEx.INSTANCE().YES);
			
			Size size = ImageUtils.getResolution(path);
			if(size.getWidth()/size.getHeight() >= _APP.widgetWidthPx/_APP.widgetHeightPx)   // 较宽
			{
				data.setOrientation(_APP.HORIZONTAL);
			}
			else
			{
				data.setOrientation(_APP.VERTICAL);
			}

			dbHelper.insert(data);
		}
	}
}
